package com.kx.controller;

import com.kx.bean.Car;
import com.kx.bean.ParkingSpace;
import com.kx.bean.User;
import com.kx.service.CarService;
import com.kx.service.ParkingSpaceService;
import com.kx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CommonController {

    @Autowired
    private UserService userService;

    @Autowired
    private CarService carService;

    @Autowired
    private ParkingSpaceService spaceService;

    @RequestMapping({"/", "login"})
    public String login() {
        return "login";
    }

    @RequestMapping("index")
    public String index() {
        return "index";
    }

//    @RequestMapping("/pay")
//    @ResponseBody
//    public int pay(Integer i){
//        i=1;
//        return i;
//    }

    @RequestMapping("jump")
    public Object jump(String url) {
        if (url.contains("user_edit")) {
            int lastIndex = url.length();
            int uid = Integer.parseInt(url.substring(9, lastIndex));
            ModelAndView mv = new ModelAndView();
            User user = userService.findById(uid);
            if (user.getCid() != null) {
                Car car = carService.findById(user.getCid());
                mv.addObject("car", car);
            }
            mv.addObject("user", user);
            mv.setViewName("user_edit");
            return mv;
        } else if (url.contains("cars_edit")) {
            int lastIndex = url.length();
            int cid = Integer.parseInt(url.substring(9, lastIndex));

            Car car = carService.findById(cid);
            ModelAndView mv = new ModelAndView();
            mv.addObject("car", car);
            mv.setViewName("cars_edit");
            return mv;
        } else if (url.contains("parking_space_edit")) {
            int lastIndex = url.length();
            int pid = Integer.parseInt(url.substring(18, lastIndex));

            ParkingSpace space = spaceService.findById(pid);
            ModelAndView mv = new ModelAndView();
            mv.addObject("space", space);
            mv.setViewName("parking_space_edit");
            return mv;
        }
        return url;
    }
}
